package net.pingfang.devicelock.Decoder;

import lombok.extern.slf4j.Slf4j;
import net.pingfang.devicelock.common.ParamCommon;
import org.springframework.integration.ip.tcp.serializer.AbstractByteArraySerializer;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;

@Slf4j
public class ByteArray7ESerializer extends AbstractByteArraySerializer {
    private static final byte SIGN = 0x7E;//起始符 和结束符

    @Override
    public byte[] serializeToByteArray(byte[] object) {
        // 添加起始和结束标志7E
        return ByteBuffer.allocate(object.length)
                .put(object)
                .array();
    }

    @Override
    public byte[] deserialize(InputStream inputStream) {
        return ParamCommon.deserialize(inputStream,SIGN);
    }


    @Override
    public void serialize(byte[] bytes, OutputStream outputStream) throws IOException {
//        outputStream.write(DELIMITER);  // 添加起始符
        outputStream.write(bytes);
//        outputStream.write(DELIMITER);  // 添加结束符
    }
}
